//菜系
$.post({
    url: './class',
    success: function (data) {
        $.each(data.data, function (index, value) {
            $("#classid_select").append("<option value=\"" + this.ClassID + "\">" + this.ClassName + "</option>")
        })
    }
})

//插入填写卡
$('#ingredients_insert').on('click', function () {
    $(this).parent().append(`<div class="formula_inout_div  mb-1 mt-1 text-justify" style="margin-left: 10%;">
              <input class="form-control form-control-lg" style="width:40%;display: inline" type="text" placeholder="配料名"
                aria-label=".form-control-lg example">
              <input class="form-control form-control-lg" style="width:40%;display: inline" type="text"
                placeholder="量，例如：一克、一颗" aria-label=".form-control-lg example">
            <button class="mt-1 rounded-circle" style="float: right;margin-right: 15px;" id="ingredients_delete">-</button>
  
            </div>`)
})

//删除填写卡
$('body').on('click', '#ingredients_delete', function () {
    $(this).parent().remove()
})

//菜单选项
$("body").on('click', '#bop', function () {
    console.log($("#class_select").val());
})

//提交菜单
$("#submit").on('click', function () {
    let key = true
    $.each($(".zzz"), function (index, value11) {
        if ($(value11).val() == null || $(value11).val() == '' || $("#classid_select").val == "蔡磊" || $("#classid_select").val == "")
            key = false
    })
    if (key) {
        var picture = new FormData();
        picture.append('file', $("#picture")[0].files[0])
        let Formula = "{"
        $.each($(".formula_inout_div"), function (index, value) {
            // if ($(value).children().eq(1).val() != '' && $(value).children().eq(0).val() != '')
            Formula += "\"" + $(value).children().eq(0).val() + "\":\"" + $(value).children().eq(1).val() + "\","
        })
        Formula = Formula.substring(0, Formula.length - 1)
        Formula += "}"
        console.log(Formula);
        $.post({
            url: '/recipe/insert',
            data: {
                FoodName: $('#foodname_input').val(),
                practice: $('#floatingTextarea2').val(),
                Formula: Formula,
                ClassID: $("#classid_select").val()
            },
            success: function (data) {
                console.log(data);
                if (data.code == 0 && $("#picture")[0].files[0]) {
                    picture.append('FoodID', data.FoodID)
                    $.ajax({
                        url: './addimg',
                        type: 'POST',
                        data: picture,
                        contentType: false,
                        processData: false,
                        success: function (data) {
                            if (data.code == 0) {
                                alert('加菜成功')
                                window.location.href = './insert'
                            }
                            else alert('加菜失败')
                        }
                    })
                } else {
                    window.location.href = './insert'
                    // console.log(data);
                    // alert("请重新登录")
                    // window.location.href = './html/login'
                }
            }
        })
    } else {
        alert('请讲输入框填写完整')
    }
})

//返回
$("#return").click(function () {
    window.location.href = './'
})